package valve_test.forms;

import valve_test.logic.valves;
import valve_test.transportation.OpenSaveXML;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.font.FontRenderContext;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.io.FilenameFilter;
import java.sql.SQLException;

/**
 * Created with IntelliJ IDEA.
 * User: Shinigami
 * Date: 15.06.14
 * Time: 20:22
 * To change this template use File | Settings | File Templates.
 */
public class start {
    private JFrame frame;
    private JMenuBar menuBar;
    public start() {
        frame = new JFrame("Valves");
        menuBar = new JMenuBar();
        JMenu menu = new JMenu("Файл");
        JMenuItem newFile = new JMenuItem("Новый");
        menu.add(newFile);
        JMenuItem open = new JMenuItem("Открыть");
        menu.add(open);
        JMenuItem save = new JMenuItem("Сохранить");
        menu.add(save);
        menu.setPreferredSize(new Dimension((int)menu.getPreferredSize().getWidth(), 20));
        menuBar.add(menu);
        JMenuItem table_valves = new JMenuItem("Добавить/редактировать");
        table_valves.setPreferredSize(new Dimension((int)table_valves.getPreferredSize().getWidth(), 20));
        menuBar.add(table_valves);
        JMenuItem graph = new JMenuItem("Графическое отображение");
        graph.setPreferredSize(new Dimension((int)graph.getPreferredSize().getWidth(), 20));
        menuBar.add(graph);
        JMenuItem check = new JMenuItem("Проверка");
        check.setPreferredSize(new Dimension((int)check.getPreferredSize().getWidth(), 20));
        menuBar.add(check);
        JMenuItem sett = new JMenuItem("Настройки");
        sett.setPreferredSize(new Dimension((int)sett.getPreferredSize().getWidth(), 20));
        menuBar.add(sett);
        JMenuItem exit = new JMenuItem("Выход");
        exit.setPreferredSize(new Dimension((int)exit.getPreferredSize().getWidth(), 20));
        menuBar.add(exit);



        table_valves.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                control.ViewValvesForm();
            }
        });

        graph.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                control.GraphValvesForm();
            }
        });

        sett.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new settings();
            }
        });

        check.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new integrity_check();
            }
        });

        newFile.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                OpenSaveXML openSaveXML = new OpenSaveXML();
                try {
                    openSaveXML.deleteAll();
                } catch (SQLException e1) {
                    e1.printStackTrace();
                }
            }
        });

        save.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                final FileDialog dialog = new FileDialog(frame, "Сохранить", FileDialog.SAVE);
                dialog.setVisible(true);
                if(!dialog.getFile().equals(null))
                {
                    final OpenSaveXML openSaveXML = new OpenSaveXML();
                    try {
                        if (openSaveXML.saveInFile(dialog.getDirectory() + dialog.getFile() + ".xml"))
                            JOptionPane.showConfirmDialog(null, "Сохранение прошло успешно", "Информация", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE );
                        else
                            JOptionPane.showConfirmDialog(null, "Не удалось сохранить файл!", "Информация", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE);

                    } catch (SQLException e1) {
                        JOptionPane.showConfirmDialog(null, "Не удалось сохранить файл!", "Информация", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE);
                    }
                }
            }
        });

        open.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                final FileDialog dialog = new FileDialog(frame, "Открыть", FileDialog.LOAD);
                dialog.setVisible(true);
                if(!dialog.getFile().equals(null))
                {
                    final OpenSaveXML openSaveXML = new OpenSaveXML();
                    try {
                        if (openSaveXML.openFromFile(dialog.getDirectory() + dialog.getFile()))
                            JOptionPane.showConfirmDialog(null, "Открытие прошло успешно", "Информация", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE );
                        else
                            JOptionPane.showConfirmDialog(null, "Не удалось открыть файл!", "Информация", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE);

                    } catch (SQLException e1) {
                        JOptionPane.showConfirmDialog(null, "Не удалось открыть файл!", "Информация", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE);
                    }
                }
            }
        });

        exit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                ExitConfirm();
            }
        });

        frame.setSize(600, 400);
        frameDisplayCenter(600, 400, frame);
        frame.setVisible(true);
        frame.setResizable(false);      //запрет на изменение размеров
        frame.setJMenuBar(menuBar);
        frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                ExitConfirm();
            }
        });
    }

    private void ExitConfirm() {
        frame.setVisible(false);
        System.exit(0);
    }

    //процедура выравнивания объекта jframe по центру экрана
    public static void frameDisplayCenter (int sizeWidth, int sizeHeight, JFrame frame) {
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int locationX = (screenSize.width - sizeWidth) / 2;
        int locationY = (screenSize.height - sizeHeight) / 2;
        frame.setBounds(locationX, locationY, sizeWidth, sizeHeight);
    }

}
